import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:namewava/utils/jc_image_path.dart';
import 'package:namewava/utils/jc_screenutil_info.dart';
import 'package:namewava/utils/jc_text_style.dart';
import 'package:table_calendar/table_calendar.dart';

class JcCalenderPop extends StatefulWidget {
  const JcCalenderPop(
      {super.key, required this.recordTimes, required this.dismissTap});
  final List recordTimes;
  final Function dismissTap;

  @override
  State<JcCalenderPop> createState() => _JcCalenderPopState();
}

class _JcCalenderPopState extends State<JcCalenderPop> {
  final kToday = DateTime.now();
  DateTime _focusedDay = DateTime.now();
  late DateTime kFirstDay;
  late DateTime kLastDay;
  TextStyle defultStyle = JcTextStyle.octoberDevanagariBoldTextStyle(
      14.sp, const Color(0xFFF7F7F7));

  TextStyle markDefultStyle = JcTextStyle.octoberDevanagariBoldTextStyle(
      14.sp, const Color(0xFF143BCB),
      fWeight: FontWeight.bold);
//CalendarController _calendarController;
  late final PageController _pageController;

  Map months = {
    '01': 'January',
    '02': 'February',
    '03': 'March',
    '04': 'April',
    '05': 'May',
    '06': 'June',
    '07': 'July',
    '08': 'August',
    '09': 'September',
    '10': 'October',
    '11': 'November',
    '12': 'December'
  };
  bool isDissMiss = false;
  getHashCode(DateTime key) {
    return key.day * 1000000 + key.month * 10000 + key.year;
  }

  @override
  void initState() {
    super.initState();
    kFirstDay = DateTime(kToday.year - 10, 0, 0);
    kLastDay = DateTime(kToday.year + 10, 0, 0);
    print(widget.recordTimes);
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _initDefault();
    });
  }

  _initDefault() {
    for (var element in widget.recordTimes) {
      DateTime day = DateTime.parse(element);
      _focusedDay = day;
    }
    setState(() {});
  }

  _nowDateStr(DateTime dateTime, {bool isEn = true}) {
    int y = dateTime.year;
    int m = dateTime.month;
    int day = dateTime.day;
    String mStr = m.toString();
    if (m < 10) {
      mStr = '0$m';
    }

    String dStr = day.toString();
    if (day < 10) {
      dStr = '0$day';
    }

    return isEn ? '${months[mStr]} $dStr $y' : '$y-$mStr-$dStr';
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        height: JcScreenutilInfo.screenHeight,
        width: JcScreenutilInfo.screenWidth,
        color: const Color.fromRGBO(0, 0, 0, 0.1),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          //  crossAxisAlignment: CrossAxisAlignment.start,
          //  mainAxisAlignment: MainAxisAlignment.center,
          children: [
            GestureDetector(
              child: Container(
                // margin: EdgeInsets.zero,
                padding: EdgeInsets.only(
                    left: 30.w,
                    right: 30.w,
                    bottom: 35.h,
                    top: JcScreenutilInfo.statusBarHeight),
                //margin: EdgeInsets.only(left: 30.w, right: 30.w),
                width: JcScreenutilInfo.screenWidth,
                // height: 400.h + JcScreenutilInfo.statusBarHeight,
                decoration: const BoxDecoration(
                    image: DecorationImage(
                        image: AssetImage(JcImagePath.jcWavaBwlRiliBg),
                        fit: BoxFit.fill)),
                child: Column(
                  children: [
                    _calenderTitle(),
                    SizedBox(
                      height: 10.h,
                    ),
                    _calenderBgWidget()
                  ],
                ),
              ),
              onTap: () {},
            )
          ],
        ),
      ),
      onTap: () {
        Get.back();
      },
    );
  }

  _calenderTitle() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Row(
          children: [
            Text(
              _nowDateStr(kToday),
              style: JcTextStyle.octoberDevanagariBoldTextStyle(
                  16.sp, Colors.white),
            ),
            InkWell(
              child: Container(
                color: Colors.transparent,
                height: 30.w,
                width: 30.w,
                child: Image.asset(JcImagePath.jcWavaRiliNext),
              ),
              onTap: () {
                _pageController.nextPage(
                  duration: const Duration(milliseconds: 300),
                  curve: Curves.easeOut,
                );
              },
            ),
            InkWell(
              child: Container(
                color: Colors.transparent,
                height: 30.w,
                width: 30.w,
                child: Image.asset(JcImagePath.jcWavaRiliPrevious),
              ),
              onTap: () {
                _pageController.previousPage(
                  duration: const Duration(milliseconds: 300),
                  curve: Curves.easeOut,
                );
              },
            )
          ],
        ),
        GestureDetector(
          child: Image.asset(
            JcImagePath.jcWavaBwlRili,
            width: 36.w,
            height: 36.w,
          ),
          onTap: () {
            Get.back();
          },
        )
      ],
    );
  }

  _calenderBgWidget() {
    return Container(
      color: Colors.transparent,
      child: TableCalendar(
        locale: 'en_US',
        firstDay: kFirstDay,
        lastDay: kLastDay,
        focusedDay: _focusedDay,
        headerVisible: false,
        daysOfWeekHeight: 20,
        onCalendarCreated: (pageController) {
          _pageController = pageController;
        },
        calendarStyle: CalendarStyle(
            outsideTextStyle: const TextStyle(color: Colors.transparent),
            todayTextStyle: defultStyle,
            rangeHighlightColor: Colors.white,
            weekendTextStyle: defultStyle,
            defaultTextStyle: defultStyle,
            selectedTextStyle: markDefultStyle,
            selectedDecoration: const BoxDecoration(
              color: Colors.white,
              shape: BoxShape.circle,
            ),
            todayDecoration: const BoxDecoration(color: Colors.transparent)),
        daysOfWeekStyle: DaysOfWeekStyle(
          weekdayStyle: defultStyle,
          weekendStyle: defultStyle,
        ),
        selectedDayPredicate: (day) {
          return widget.recordTimes.contains(_nowDateStr(day, isEn: false));
        },
        onDaySelected: (selectedDay, focusedDay) {
          // print(focusedDay);
          // _focusedDay = focusedDay;

          // setState(() {});
          widget.dismissTap(_nowDateStr(selectedDay, isEn: false));
          Get.back();
        },
        onFormatChanged: (format) {},
        onPageChanged: (focusedDay) {
          // No need to call `setState()` here
          _focusedDay = focusedDay;
        },
      ),
    );
  }
}
